package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rules.admin.dao.ReportErrLogModelDao;
import com.rules.admin.dao.ReportModelDao;
import com.rules.admin.domain.Page;
import com.rules.admin.entity.ReportConditionModel;
import com.rules.admin.entity.ReportErrLogModel;
import com.rules.admin.entity.ReportModel;
import com.rules.admin.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;

import java.util.Iterator;
import java.util.List;

@Service
public class ReportServiceImpl extends ServiceImpl<ReportModelDao, ReportModel> implements ReportService {

   @Autowired
   private ReportErrLogModelDao reportErrLogModelDao;


   public Page getPage(ReportConditionModel model, Page page) {
      List<ReportModel> list = this.getList(model);
      this.setPage(list, page);
      return page;
   }

   public List<ReportModel> getList(ReportConditionModel model) {
      this.setType(model);
      List<ReportModel> list = this.baseMapper.getRecords(model);
      Iterator var3 = list.iterator();

      while(var3.hasNext()) {
         ReportModel reportModel = (ReportModel)var3.next();
         if (reportModel.getNormalCount() != 0L) {
            reportModel.setNormalExecutionTime(reportModel.getNormalExecutionTime() / reportModel.getNormalCount());
         }
      }

      return list;
   }


   public Page getErrLog(ReportConditionModel model, Page page) {
      List<ReportErrLogModel> list = this.baseMapper.getErrRecords(model);
      this.setPage(list, page);
      return page;
   }

   public List<ReportModel> getReports(ReportConditionModel model) {
      this.setType(model);
      List<ReportModel> list = this.baseMapper.getReports(model);
      Iterator var3 = list.iterator();

      while(var3.hasNext()) {
         ReportModel reportModel = (ReportModel)var3.next();
         if (reportModel.getNormalCount() != 0L) {
            reportModel.setNormalExecutionTime(reportModel.getNormalExecutionTime() / reportModel.getNormalCount());
         }
      }

      return list;
   }



   private void setType(ReportConditionModel model) {
      String dialect = (String)ContextLoader.getCurrentWebApplicationContext().getServletContext().getAttribute("dialect");
      if (dialect.equals("ORACLE")) {
         if ("minute".equals(model.getType())) {
            model.setSearchType("yyyy-MM-dd hh24:mi");
         } else if ("hour".equals(model.getType())) {
            model.setSearchType("yyyy-MM-dd hh24");
         } else if ("month".equals(model.getType())) {
            model.setSearchType("yyyy-MM");
         } else {
            model.setSearchType("yyyy-MM-dd");
         }
      } else if ("minute".equals(model.getType())) {
         model.setSearchType("%Y-%m-%d %H:%i");
      } else if ("hour".equals(model.getType())) {
         model.setSearchType("%Y-%m-%d %H");
      } else if ("month".equals(model.getType())) {
         model.setSearchType("%Y-%m");
      } else {
         model.setSearchType("%Y-%m-%d");
      }

   }

   private void setPage(List list, Page page) {
      int start = page.getPageSize() * (page.getCurrentPage() - 1);
      if (start > list.size()) {
         list.clear();
      } else if (list.size() < page.getPageSize() * page.getCurrentPage()) {
         list = list.subList(start, list.size());
      } else {
         list = list.subList(start, page.getPageSize() * page.getCurrentPage());
      }

      long totalCount = (long)list.size();
      page.setRecords(list);
      page.setTotalRecord((int)totalCount);
   }
}
